package com.gitee.feizns.explore.data_structure.tree.bs.mid;

/**
 * @author feizns
 * @since 2020/3/10
 */
public class ConstructMaximumBinaryTree {

    public static void main(String[] args) {

    }

    public static TreeNode constructMaximumBinaryTree(int[] nums) {
        return buildTree(nums, 0, nums.length - 1);
    }

    private static TreeNode buildTree(int[] num, int l, int r) {
        TreeNode ret = null;
        if ( l == r )
            ret = new TreeNode(num[l]);
        else if ( l < r ) {
            int max = l;
            for (int i = l + 1; i <= r; i++) {
                if ( num[i] > num[max] )
                    max = i;
            }
            ret = new TreeNode(num[max]);
            ret.left = buildTree(num, l, max - 1);
            ret.right = buildTree(num, max + 1, r);
        }
        return ret;
    }

}
